import upload from "./upload.js";
fetch("country-add.html").then(r=>r.text()).then(text=>{
   let addDialog = $(text);
   console.debug(addDialog);
   $("body").append(addDialog);


let form = addDialog[0].querySelector("form");

let btn = addDialog[0].querySelector("._save");


console.debug(addDialog,form,btn);

   upload(addDialog.find("._upload")[0],function(data){
      form.countryUri.value=data.uri;
   });

btn.onclick = function(){
   let params = new URLSearchParams(new FormData(form));
   console.debug(params.toString());
   fetch("country-add.let",{
      method: 'POST', // *GET, POST, PUT, DELETE, etc.
      mode: 'cors', // no-cors, *cors, same-origin
      cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
      credentials: 'include', // include, *same-origin, omit
      headers: {
         // 'Content-Type': 'application/json'
         'Content-Type': 'application/x-www-form-urlencoded',
      },
      redirect: 'follow', // manual, *follow, error
      referrerPolicy: 'no-referrer', // no-referrer, *no-referrer-when-downgrade, origin, origin-when-cross-origin, same-origin, strict-origin, strict-origin-when-cross-origin, unsafe-url
      // body: JSON.stringify(data) // body data type must match "Content-Type" header
      body:params
   }).then(r=>r.json()).then((data)=>{
      console.debug(data);
      if(data.ok){
         addDialog.modal("hide");
         form.reset();//重置
         window.postMessage("loadData");//发送消息
      }else{
         alert(data.msg);
      }
   }).catch(e=>{
      console.error(e);
   });
}
});
export default function (){
   // addDialog.modal("show");
}
